<?php
if (!isset($model))
    $model = new BDynamicFormModel ();
$venderNameField = 'media_name_vender_info';
$myVenderIdField = 'media_myVenderId_vender_info';
$model->addField($venderNameField);
$model->addField($myVenderIdField);

$attributeNameField = 'media_name_vender_attribute';
$attributeAliasField = 'media_alias_vender_attribute';
$attributeTypeField = 'media_type_vender_attribute';
$model->addField($attributeNameField);
$model->addField($attributeAliasField);
$model->addField($attributeTypeField);

$venderAttributeView = array();

if ($venderId > 0) {
    $venderTable = new BPSheet('vender');
    $vender = $venderTable->getListById($venderId);

    $venderAttributeTable = new BPSheet('venderAttribute', $vender);
    $venderAttributeView = $venderAttributeTable->getView();

    /**
     * below code will populate attribute for product
     */
    foreach ($venderAttributeView as $id => $venderAttribute) {
        $nameItem = $venderAttribute->getItem('name');
        $typeItem = $venderAttribute->getItem('type');
        if ($nameItem instanceof BTreeItem) {
            $name = $nameItem->getValue();
            $attributeField = 'media_' . $name . '_vender_info';
            $model->addField($attributeField);
            //get value of attribute in product 
            $attributeItem = $vender->getItem($name);
            if ($attributeItem instanceof BTreeItem) {
                $attributeValue = $attributeItem->getValue();
                $model->$attributeField = $attributeValue;
            }
            else
                $model->$attributeField = '';
        }
        if ($typeItem instanceof BTreeItem) {
//            $type = $nameItem->getValue();
//            $attributeField = 'media_' . $type . '_vender_info';
            $type = $typeItem->getValue();
            if($type == 'image')
                $attributeField = 'media_' . $type . '_vender_info';
            $model->addField($attributeField);
            $attributeItem = $vender->getItem($type);
            if ($attributeItem instanceof BTreeItem) {
                $attributeValue = $attributeItem->getValue();
                $model->$attributeField = $attributeValue;
            }
        }
    }

    $venderNameItem = $vender->getItem('name');
    if ($venderNameItem instanceof BTreeItem) {
        $venderName = $venderNameItem->getValue();
        $model->$venderNameField = $venderName;
    }
    $myVenderIdItem = $vender->getItem('myVenderId');
    if ($myVenderIdItem instanceof BTreeItem) {
        $myVenderId = $myVenderIdItem->getValue();
        $model->$myVenderIdField = $myVenderId;
    }
}
?>
<?php
$app = Blyn::app()->getActiveApp();
?>


<link rel="stylesheet" type="text/css"
      href="<?php echo Yii::app()->request->baseUrl; ?>/css/bootstrap-datetimepicker.min.css" />
<script  type="text/javascript"
         src="<?php echo Yii::app()->request->baseUrl; ?>/js/bootstrap-datetimepicker.min.js"/>
<script type="text/javascript">
    $(function() {
        $('#datetimepicker1').datetimepicker({
            language: 'pt-BR'
        });
    });</script>
<div class="well-small row-fluid">
    <div class="input-prepend span3">
        <span class="add-on">属性名</span>
        <?php
        echo CHtml::activeTextField($model, $attributeNameField, array('placeholder' => 'attribute' . '...', 'required' => true, 'class' => 'span9'));
        ?>
    </div>
    <div class="input-prepend span3">
        <span class="add-on">别名</span>
        <?php
        echo CHtml::activeTextField($model, $attributeAliasField, array('placeholder' => 'alias' . '...', 'required' => true, 'class' => 'span9'));
        ?>
    </div>
    <div class="input-prepend span3">
        <span class="add-on">类型</span>
        <?php
        $typeList = $app->getDataTypeList();
        echo CHtml::activeDropDownList($model, $attributeTypeField, $typeList, array('required' => true, 'class' => 'span8'));
        ?>
    </div>
    <div class="input-append span2">
        <?php
        echo $control->getAjaxLink(array('name' => '添加属性', 'id' => 'addVenderAttribute'), array(
            'htmlOptions' => array('class' => 'btn'),
            'venderId' => $venderId,
            'eventId' => $eventId,
            'attributeId' => 0
        ));
        ?>
    </div>
</div>
<table class="table table-hover">
    <colgroup>
        <col class="span2">
        <col class="span4">
        <col class="span4">
    </colgroup>
    <thead>
        <tr>
            <th><?php echo '属性'; ?></th>
            <th><?php echo '内容'; ?></th>
            <th><?php echo '操作'; ?></th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>
                <?php echo '供应商名称'; ?>
            </td>
            <td>
                <?php
                echo CHtml::activeTextField($model, $venderNameField, array('placeholder' => 'vender name' . '...', 'required' => true));
                ?>
                <!--                    &nbsp;&nbsp;&nbsp;&nbsp;<a id="CreateNewType_clink" role="button" data-toggle="modal" class="btn disabled" style="font-size: 12px;margin-top: -12px;">创建新类型</a>-->
            </td>
            <td></td>
        </tr>
        <tr><td> <?php echo '供应商ID'; ?> </td>
            <td>
                <?php
                echo CHtml::activeTextField($model, $myVenderIdField, array('placeholder' => 'vender id' . '...', 'required' => true));
                ?>
            </td>
            <td></td>
        </tr>               

        <?php
        if ($venderId > 0) {
            foreach ($venderAttributeView as $venderAttribute) {
                $attributeId = $venderAttribute->getId();
                ?>
                <tr>
                    <?php
                    $nameItem = $venderAttribute->getItem('name');
                    $typeItem = $venderAttribute->getItem('type');
                    $aliasItem = $venderAttribute->getItem('alias');
                    $name = '';
                    $type = '';
                    $alias = '';
                    $attributeValue = '';
                    if ($nameItem instanceof BTreeItem) {
                        $name = $nameItem->getValue();
                        $attributeField = 'media_' . $name . '_vender_info';
                        $attributeItem = $vender->getItem($name);
                        if ($attributeItem instanceof BTreeItem) {
                            $attributeValue = $attributeItem->getValue();
                            $model->$attributeField = $attributeValue;
                        }
                        else
                            $model->$attributeField = '';
                    }
                    if ($typeItem instanceof BTreeItem) {
                        $type = $typeItem->getValue();
                        //get typeData from define in attribute
                        $typeData = $app->getModelDataByDataType($typeItem);
                    }
                    if ($aliasItem instanceof BTreeItem) {
                        $alias = $aliasItem->getValue();
                    }
                    if ($alias == '')
                        $alias = $name;
                    ?>
                    <td> <?php echo $alias; ?>  </td>   
                    <td> <?php
                        $modelData = array();
                        $modelData['model'] = $model;
                        $modelData['field'] = $attributeField;
                        $modelData['data'] = $typeData;
                        if (isset($form))
                            $modelData['form'] = $form;
                        echo $app->showActiveFieldByDataType($type, $modelData);
                        ?>  
                    </td>
                    <td>
                        <?php
                        echo $control->getAjaxLink(array('name' => '删除', 'id' => 'deleteVenderAttribute'), array(
                            'htmlOptions' => array('class' => 'btn'),
                            'venderId' => $venderId,
                            'attributeId' => $attributeId
                        ));
                        ?>
                    </td>
                </tr>
                <?php
            }
        }
        ?>
    </tbody>
</table>

